'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 主城小玩法
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi.js');
var rewardService = require('../../../services/rewardService');
var smallPlayConf = require('../../../config/smallPlayConf.js');
var roleService = require('../../../services/roleService.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 领取主城小玩法 ----  打猎奖励
 */
Handler.prototype.receiveHunt = function (msg, session, next) {
    var role_id = session.uid;
    var hunt_id = msg.hunt_id;
    if (!hunt_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var hunt_list = [];
    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'vip', 'build', 'hunt', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var vip_model = player_manager.vip;
                var role_model = player_manager.role;
                var hunt_model = player_manager.hunt;

                // 怪物存在就移除
                if (!hunt_model.removeHunt(hunt_id)) {
                    return next(null, {
                        code: Code.HUNT_NOT_HUNT
                    });
                }
                hunt_list = hunt_model.getHuntList();

                // 取系数
                var food_ratio = dataApi.hunt.findById(hunt_id).food_ratio || 1;

                // 发放奖励
                var add_food = smallPlayConf.getLvRewardConf(role_model.getLv()) * food_ratio;
                roleService.addFoodLimit(player_manager, moduleType.MDL_BAG, operationType.OPR_BAG_USE_CONSUME, add_food);
                reward_list.push({
                    hole_num: 0,
                    reward: 0,
                    reward_num: add_food,
                    reward_type: 12,
                    reward_value: 0
                });
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    hunt_list: hunt_list,
                    reward_list: reward_list
                }
            });
        }
    );
};

/**
 * 领取主城小玩法 ----  内城宝箱奖励
 */
Handler.prototype.receiveChest = function (msg, session, next) {
    var role_id = session.uid;
    var reward_list = [];
    var chest_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'vip', 'hunt', 'build', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var vip_model = player_manager.vip;
                var role_model = player_manager.role;
                var hunt_model = player_manager.hunt;

                // 宝箱是否可以领取
                if (!hunt_model.isChest()) {
                    return next(null, {
                        code: Code.HUNT_NOT_CHEST
                    });
                }

                // 移除队列
                hunt_model.removeChest();

                // 发放奖励
                var add_food = smallPlayConf.getLvRewardConf(role_model.getLv());
                roleService.addFoodLimit(player_manager, moduleType.MDL_OTHER, operationType.OPR_OTHER_HUNT, add_food);
                reward_list.push({
                    hole_num: 0,
                    reward: 0,
                    reward_num: add_food,
                    reward_type: 12,
                    reward_value: 0
                });
                var reward_item = dataApi.city_box.findById(1);
                var random_num = _.random(0, 100);
                if (random_num !== reward_item.probability) {
                    rewardService.addRewardToRole(player_manager, reward_item, moduleType.MDL_OTHER, operationType.OPR_OTHER_HUNT);
                    reward_list.push(reward_item);
                }
                chest_list = hunt_model.getHuntList();
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    chest_list: chest_list,
                    reward_list: reward_list
                }
            });
        }
    );
};
